/**
 * 设备检测工具
 * 检测设备类型、浏览器、操作系统等信息
 */

/**
 * 获取设备类型
 * @returns {string} PC | Mobile | Tablet
 */
export function getDeviceType() {
  const ua = navigator.userAgent
  if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(ua)) {
    return 'Tablet'
  }
  if (/Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test(ua)) {
    return 'Mobile'
  }
  return 'PC'
}

/**
 * 获取浏览器信息
 * @returns {string} 浏览器名称和版本
 */
export function getBrowserInfo() {
  const ua = navigator.userAgent
  let browser = 'Unknown'
  let version = ''

  if (ua.indexOf('Edg') > -1) {
    browser = 'Edge'
    version = ua.match(/Edg\/([\d.]+)/)?.[1] || ''
  } else if (ua.indexOf('Chrome') > -1 && ua.indexOf('Edg') === -1) {
    browser = 'Chrome'
    version = ua.match(/Chrome\/([\d.]+)/)?.[1] || ''
  } else if (ua.indexOf('Firefox') > -1) {
    browser = 'Firefox'
    version = ua.match(/Firefox\/([\d.]+)/)?.[1] || ''
  } else if (ua.indexOf('Safari') > -1 && ua.indexOf('Chrome') === -1) {
    browser = 'Safari'
    version = ua.match(/Version\/([\d.]+)/)?.[1] || ''
  } else if (ua.indexOf('MSIE') > -1 || ua.indexOf('Trident') > -1) {
    browser = 'IE'
    version = ua.match(/(?:MSIE |rv:)([\d.]+)/)?.[1] || ''
  }

  return version ? `${browser} ${version}` : browser
}

/**
 * 获取操作系统信息
 * @returns {string} 操作系统名称和版本
 */
export function getOSInfo() {
  const ua = navigator.userAgent
  let os = 'Unknown'

  if (ua.indexOf('Win') > -1) {
    os = 'Windows'
    if (ua.indexOf('Windows NT 10.0') > -1) os = 'Windows 10/11'
    else if (ua.indexOf('Windows NT 6.3') > -1) os = 'Windows 8.1'
    else if (ua.indexOf('Windows NT 6.2') > -1) os = 'Windows 8'
    else if (ua.indexOf('Windows NT 6.1') > -1) os = 'Windows 7'
  } else if (ua.indexOf('Mac') > -1) {
    os = 'macOS'
  } else if (ua.indexOf('Linux') > -1) {
    os = 'Linux'
  } else if (ua.indexOf('Android') > -1) {
    os = 'Android'
    const version = ua.match(/Android ([\d.]+)/)?.[1]
    if (version) os = `Android ${version}`
  } else if (ua.indexOf('iOS') > -1 || ua.indexOf('iPhone') > -1 || ua.indexOf('iPad') > -1) {
    os = 'iOS'
    const version = ua.match(/OS ([\d_]+)/)?.[1]?.replace(/_/g, '.')
    if (version) os = `iOS ${version}`
  }

  return os
}

/**
 * 获取设备名称
 * @returns {string} 设备名称
 */
export function getDeviceName() {
  const os = getOSInfo()
  const browser = getBrowserInfo()
  const deviceType = getDeviceType()

  if (deviceType === 'PC') {
    return `${os} ${browser.split(' ')[0]}`
  } else if (deviceType === 'Mobile') {
    return `${os} ${browser.split(' ')[0]}`
  } else {
    return `${os} Tablet`
  }
}

/**
 * 获取完整的设备信息
 * @returns {Object} 设备信息对象
 */
export function getDeviceInfo() {
  return {
    deviceName: getDeviceName(),
    deviceType: getDeviceType(),
    browser: getBrowserInfo(),
    os: getOSInfo(),
    userAgent: navigator.userAgent,
    language: navigator.language,
    screenResolution: `${screen.width}x${screen.height}`,
    colorDepth: screen.colorDepth,
    timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
    timezoneOffset: new Date().getTimezoneOffset()
  }
}
